package com.hc.file.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import com.hc.bean.BaseStationBean;
import com.hc.util.CommonData;


public class BaseStationFileUtil {

	public static void  readBaseStationFile(){
		
	File file = new File(CommonData.systemPath + "config"+File.separatorChar+"baseStationInfo.property");
		
		FileReader fileReader = null;

			try {
				fileReader = new  FileReader(file);
			} catch (FileNotFoundException e1) {
				e1.printStackTrace();
			}
			BufferedReader reader = new BufferedReader(fileReader);
			String linStr = "";
			BaseStationBean baseStationBean = null;
			try {
				while((linStr=reader.readLine())!=null){
					
				String[] results= 	linStr.split("=");
					
					if("id".equals(results[0])){
						baseStationBean = new BaseStationBean();
						baseStationBean.setId(Integer.parseInt(results[1]));
						CommonData.baseStationBeans.put(baseStationBean.getId(), baseStationBean);
					}else if("name".equals(results[0])){
						
						baseStationBean.setName(results[1]);
						
					}else if("x".equals(results[0])){
						
						baseStationBean.setX(Integer.parseInt(results[1]));
						baseStationBean.setStartX(Integer.parseInt(results[1]));
					}else if("y".equals(results[0])){
						
						baseStationBean.setY(Integer.parseInt(results[1]));
						baseStationBean.setStartY(Integer.parseInt(results[1]));
						
					}else if("baseStation_tempNumber".equals(results[0])){
						
						baseStationBean.setBaseStationTempNumber(Integer.parseInt(results[1]));
						
					}else if("isUseBaseStation_FH".equals(results[0])){
						
						baseStationBean.setUseBaseStationFH(results[1].equals("true"));
						
					}else if("baseStation_FH".equals(results[0])){
						
						baseStationBean.setBaseStationFH(Integer.parseInt(results[1]));
						
						
					}else if("baseStationJN".equals(results[0])){
						baseStationBean.setBaseStationJN(Integer.parseInt(results[1]));
						
					}
				}
				
				reader.close();
				fileReader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		
			
		
			
	}
	public static void saveBaseStationBeans()
	{
		
		StringBuilder sb = new StringBuilder();
		
		
		
	for(Object ob:CommonData.baseStationBeans.keySet()){
			
			BaseStationBean bean = CommonData.baseStationBeans.get(ob);
			String lineSeparator = System.getProperty("line.separator", "\n");  
			sb.append("id="+bean.getId()+lineSeparator);
			sb.append("name="+bean.getName()+lineSeparator);
			sb.append("x="+bean.getStartX()+lineSeparator);
			sb.append("y="+bean.getStartY()+lineSeparator);
			
			sb.append("baseStation_tempNumber="+bean.getBaseStationTempNumber()+"\r\n");
			sb.append("isUseBaseStation_FH="+bean.isUseBaseStationFH()+"\r\n");
			sb.append("baseStation_FH="+bean.getBaseStationFH()+"\r\n");
			sb.append("baseStationJN="+bean.getBaseStationJN()+"\r\n");
		}
		
		
		File file = new File(CommonData.systemPath + File.separator+"config"+File.separator+"baseStationInfo.property");
		
		
		try {
			FileWriter w = new FileWriter(file);
			w.write(sb.toString());
			w.flush();
			w.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
		
	}
	
	public static void main(String[] args) {
		BaseStationFileUtil.readBaseStationFile();
		
	StringBuilder sb = new StringBuilder();
		
		
		
		for(Object ob:CommonData.baseStationBeans.keySet()){
			
			BaseStationBean bean = CommonData.baseStationBeans.get(ob);
			String lineSeparator = System.getProperty("line.separator", "\n");  
			sb.append("id="+bean.getId()+lineSeparator);
			sb.append("name="+bean.getName()+lineSeparator);
			sb.append("x="+bean.getX()+lineSeparator);
			sb.append("y="+bean.getY()+lineSeparator);
			
		}
		
		System.out.println(sb.toString());
		
		BaseStationFileUtil.saveBaseStationBeans();
		
	}
	
}
